package com.pys.beans;

import java.io.Serializable;
import java.util.Locale;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.pys.pojo.PysKullanici;
import com.pys.service.KullaniciService;
import com.pys.utils.FacesUtils;

@ManagedBean(eager = true)
@SessionScoped
public class UserSessionBean implements Serializable {

	private static final Logger LOGGER = LoggerFactory
			.getLogger(UserSessionBean.class);

	public static final String HOME_PAGE_REDIRECT = "/main.jsf?faces-redirect=true";
	public static final String LOGOUT_PAGE_REDIRECT = "/logout.jsf?faces-redirect=true";

	private String mail;
	private String sifre;
	private boolean hatirla;
	private PysKullanici kullanici;

	public UserSessionBean() {
		kullanici = null;
	}

	public String getMail() {
		return mail;
	}

	public void setMail(String mail) {
		this.mail = mail;
	}

	public String getSifre() {
		return sifre;
	}

	public void setSifre(String sifre) {
		this.sifre = sifre;
	}

	public boolean isHatirla() {
		return hatirla;
	}

	public void setHatirla(boolean hatirla) {
		this.hatirla = hatirla;
	}

	/*
	 * ACTIONS
	 */

	public String login() {
		// lookup the user based on user name and user password
		KullaniciService kullaniciService = new KullaniciService();
		kullanici = kullaniciService.checkValidUser(mail, sifre);

		if (isLoggedIn()) {
			// LOGGER.info("login successful for '{}'", kullanici.getMail());
			// Flash flash = FacesUtils.getFlash();
			// flash.clear();
			return HOME_PAGE_REDIRECT;
			// loggedIn = true;
			// return "index.jsf?faces-redirect=true";
		} else {
			LOGGER.info("login failed for '{}'");
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_WARN,
							"Login failed", "Invalid or unknown credentials."));

			return null;
			// FacesUtils.addMessage(FacesMessage.SEVERITY_ERROR,
			// "ERROR_LOGIN");
			// return null;
		}
	}

	public String logout() {
		String identifier = kullanici.getMail();

		// invalidate the session
		LOGGER.debug("invalidating session for '{}'", identifier);
		FacesContext.getCurrentInstance().getExternalContext()
				.invalidateSession();

		LOGGER.info("logout successful for '{}'", identifier);
		return LOGOUT_PAGE_REDIRECT;
	}

	public boolean isLoggedIn() {
		return kullanici != null;
	}

	public String isLoggedInForwardHome() {
		if (isLoggedIn()) {
			return HOME_PAGE_REDIRECT;
		}

		return null;
	}

	public PysKullanici getCurrentUser() {
		return kullanici;
	}

	public void handleException(Throwable exception) {

	}

	private static final Logger logger = LoggerFactory
			.getLogger(UserSessionBean.class);

	private Locale locale;
	boolean hataVarmi;

	public boolean isHataVarmi() {
		return FacesUtils.getFacesContext().getMessages().hasNext();
	}

	public Locale getLocale() {
		if (this.locale == null)
			return new Locale("tr");
		return locale;
	}

	public void setLocale(Locale locale) {
		this.locale = locale;
	}

	// do not provide a setter for currentUser!
}
